package com.wgt;

import java.io.*;
import java.util.*;

/**
 * Created by wgt on 2017/5/31.
 */
public class ReadName {

    public static void main(String[] args) throws IOException {
        readName();
    }

    /**
     * 从类似如下的文本文件中读取出所有的姓名，并打印出重复的姓名和重复的次数，并按重复次数排序
     * @throws IOException
     */
    public static void readName() throws IOException {
        File file = new File("F:\\File\\2017\\name.txt");
        BufferedReader bf_reader = new BufferedReader(new FileReader(file));
        String content = null;
        List<User> users = new ArrayList<>();
        User user = null;
        while ((content=bf_reader.readLine())!=null){
            String []strings = content.split(",");
            String name = strings[1];
            user=new User(name);
            if (users.contains(user)){
                int index = users.indexOf(user);
                users.get(index).add();
            }else{
                users.add(user);
            }
        }
        Collections.sort(users);
        System.out.println(users);
        bf_reader.close();
    }

}


class User implements Comparable<User>{

    private String name;
    private Integer count;


    public User(String name) {
        this.name = name;
        this.count = 1;
    }

    public void add(){
        this.count++;
    }




    @Override
    public boolean equals(Object obj) {
        return this.name.equals(((User)obj).name);
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", count=" + count +
                '}';
    }

    @Override
    public int compareTo(User o) {
        return this.count>o.count?-1:(this.count==o.count)?0:1;
    }
}